<template>
  <van-icon v-if="value" name="star" @click="changeCollect"></van-icon>
  <van-icon v-else name="star-o" @click="changeCollect"></van-icon>
</template>

<script>
import { toCollect, cancelCollect } from '@/api/user.js'
export default {
  name: 'Collect',
  props: {
    value: {
      type: Boolean,
      required: true
    },
    articleId: {
      type: [Object, Number, String],
      default: null
    }
  },
  data() {
    return {}
  },
  updated() {
    // console.log(this.articleId);
  },
  methods: {
    async changeCollect() {
      this.$toast.loading({
        duration: 0, // 持续展示 toast
        message: '操作中...',
        forbidClick: true // 是否禁止背景点击
      })
      try {
        if (this.value) {
          // 已收藏， 要取消
          await cancelCollect(this.articleId)
          this.$toast.success('取消成功')
        } else {
          // 未收藏， 要收藏
          await toCollect(this.articleId)
          this.$toast.success('收藏成功')
        }
        this.$emit('input', !this.value)
      } catch (error) {
        this.$toast.fail('操作失败')
      }
    }
  }
}
</script>

<style scope lang="less">
.van-icon-star {
  color: orange !important;
}
</style>
